package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Book;
import com.example.service.IBookService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private IBookService iBookService;
    //分页查询
    @PostMapping("/PageList/{currentPage}/{pageSize}")
    public IPage<Book> listPage(@PathVariable int currentPage,@PathVariable int pageSize, @RequestBody Book book){
        IPage<Book> ipage=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Book> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(Strings.isNotEmpty(book.getName()),Book::getName,book.getName());
        wrapper.like(Strings.isNotEmpty(book.getAddress()),Book::getAddress,book.getAddress());
        IPage<Book> page = iBookService.page(ipage, wrapper);
        return page;
    }
    //id查询
    @GetMapping("/selectId/{id}")
    public Book selectId(@PathVariable Long id){
        Book book = iBookService.getById(id);
        return book;
    }
    //行修改
    @PostMapping("/updateRow")
    public Boolean selectId(@RequestBody Book book){
        boolean flag = iBookService.updateById(book);
        return flag;
    }
    //行删除
    @DeleteMapping("/deleteRow/{id}")
    public Boolean deleteRow(@PathVariable Long id){
        boolean flag = iBookService.removeById(id);
        return flag;
    }
    //多行删除
    @PostMapping("/delete")
    public Boolean delete(@RequestBody List<Book> list){
        List<Long> ids=new ArrayList<>();
        for (Book book : list) {
            ids.add(book.getId());
        }
        boolean flag = iBookService.removeByIds(ids);
        return flag;
    }
    @PutMapping("/addRow")
    public Boolean addRow(@RequestBody Book book){
        boolean flag = iBookService.save(book);
        return flag;
    }
}
